<!-- 注册页面 => 注册组件 -->
<template>
  <div class="register-comp">
    <div class="contianer">
      <div class="logo-box">
        <img src="@/assets/logo.png" alt="" />
        <span>商城后台用户注册</span>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="adminname">
          <el-input
            v-model="ruleForm.adminname"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            placeholder="请输入六位数密码"
            autocomplete="off"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="checkedKeys">
          <el-input
            type="password"
            v-model="ruleForm.checkedKeys"
            placeholder="请重复输入密码"
            autocomplete="off"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <div class="register-link">
          <el-link type="primary" @click="handleToLogin"
            >已有账户？登录</el-link
          >
        </div>

        <el-form-item>
          <el-button type="primary" @click="submitForm">注册</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
        <div class="title-footer">登录注册代表您已阅读并同意《服务协议》</div>
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入注册接口
import { register } from "@/api/user";
export default {
  name: "RegisterComp",
  components: {},
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 6) {
        callback(new Error("密码长度必须大于6"));
        return;
      } else {
        if (this.ruleForm.checkedKeys !== "") {
          this.$refs.ruleForm.validateField("checkedKeys");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value.length < 6) {
        callback(new Error("密码长度必须大于6"));
        return;
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        adminname: "",
        password: "",
        checkedKeys: "",
        role: 0,
      },
      rules: {
        adminname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 6, message: "用户名必须大于6个字符", trigger: "blur" },
        ],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkedKeys: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    handleToLogin() {
      this.$emit("toggleComponet", "LoginComp");
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log("register", this.ruleForm);
          // alert("submit!");
          register(this.ruleForm).then((res) => {
            console.log(res);
            if (res.code === "200") {
              this.$emit("toggleComponet", "LoginComp");
            }
          });
        } else {
          console.log("注册失败!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped lang="scss">
// element input 样式修改
:deep(.el-form .el-form-item) {
  display: flex;
  justify-content: center;
}
:deep(.el-form .el-form-item .el-form-item__content) {
  margin-left: 0 !important;
  width: 100% !important;
}
:deep(.el-button--primary) {
  width: 100%;
  border-radius: 20px;
  background: linear-gradient(to right, #c236bd, #6d02c0, #6900bf);
  border: none;
}
:deep(.el-button--primary):hover {
  opacity: 0.8;
}
* {
  user-select: none;
}
.register-comp {
  width: 800px;
  height: 500px;
  background: url("@/assets/login.png") no-repeat;
  background-size: 100% 100%;
  position: relative;

  .contianer {
    box-sizing: border-box;
    height: 400px;
    width: 340px;
    padding: 0 10px;
    // background-color: pink;
    position: absolute;
    right: 20px;
    top: 40px;
    .logo-box {
      box-sizing: border-box;
      width: 100%;
      height: 60px;
      padding: 10px 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #000;
      margin-bottom: 20px;

      img {
        height: 100%;
      }
    }

    .register-link {
      font-size: 12px;
      a {
        font-size: 12px;
        position: relative;
        top: -5px;
      }
    }
    .title-footer {
      margin-top: 30px;
      font-size: 12px;
      color: #888;
      text-align: center;
    }
  }
}
</style>
